<?php use_helper('Pagination'); ?>
<?php $currentSeason = $seasonStart . ' - ' . ($seasonStart+1); ?>
<?php //$mensualPoint = LicenseePoint::getMensualPoints($licensee_point_list); ?>
<?php
    $licenseePointByMonthArray = array();
    foreach($licensee_point_list as $licenseePoint){
        if(!empty($licenseePoint[ 'nb_points' ])){
            $licenseePointByMonthArray[strtotime($licenseePoint[ 'datetime_points' ])] = intval($licenseePoint[ 'nb_points' ]);
        }else if(!empty($licenseePoint[ 'nb_points_fftt' ])){
            $licenseePointByMonthArray[strtotime($licenseePoint[ 'datetime_points' ])] = intval($licenseePoint[ 'nb_points_fftt' ]);
        }
    }
?>

<?php include_partial('member/profilMenuTop', array('member_info' => $member_info, 'selected' => 2)); ?>

<h1 class="title">Matchs joués saison <?php echo $currentSeason ?></h1>
<p>
    <select onChange="window.location.href=this.options[this.selectedIndex].value">
        <option value="">Visualisez une autre saison : </option>
        <?php $min_date = date('Y', strtotime($season_list['min_date'])) ?>
        <?php $max_date = date('Y', strtotime($season_list['max_date'])) ?>

        <?php for($i=$min_date; $i<$max_date+1; $i++): ?>

            <?php
                $link = "";
                if($sf_user->isAuthenticated() && $sf_user->getGuardUser()->getId() == $member_info[ 'sfGuardUserProfile' ][ 'User' ][ 'id' ]):
                    $link = url_for('@member_myMatchList_changeSeason?seasonStart=' . $i . '&seasonEnd=' . ($i+1));
                else:
                    $link = url_for('@member_matchList_changeSeason?name_slug=' . $member_info[ 'slug' ]. '&seasonStart=' . $i . '&seasonEnd=' . ($i+1));
                endif;
            ?>

            <option value="<?php echo $link ?>">Saison <?php echo $i ?> - <?php echo $i+1 ?></option>
        <?php endfor; ?>

    </select>
</p>

<?php if($sf_user->isAuthenticated() && $sf_user->getGuardUser()->getId() == $member_info[ 'sfGuardUserProfile' ][ 'User' ][ 'id' ]){
    echo link_to('Enregistrer des matchs', '@member_save_matchList', array('class' => 'saveMatch box_relief dark_blue'));
}?>

<?php
    $nbMatchs = $pager->getNbResults();
    $victoryPerc = $nbMatchs > 0 ? $nbVictory * 100/$nbMatchs : 0;
    $defeatPerc = $nbMatchs > 0 ? ($nbMatchs - $nbVictory) * 100/$nbMatchs : 0;
?>

<table class="list-match percent">
    <tr>
        <th>Parties disputées :</th>
        <td><?php echo $nbMatchs ?></td>
    </tr>
    <tr>
        <th>Gagnées :</th>
        <td><span class="victory"><?php echo number_format($victoryPerc, 1) ?>%</span></td>
    </tr>
    <tr>
        <th>Perdus :</th>
        <td><span class="defeat"><?php echo number_format($defeatPerc, 1) ?>%</span></td>
    </tr>
</table>

<div class="push_float"></div>

<table class="list-match">
    <thead>
        <tr>
            <th rowspan="2">Position</th>
            <th rowspan="2"><span class="victory">Victoire</span><br/><span class="defeat">Défaite</span></th>
            <th rowspan="2">Date</th>
            <th colspan="2" class="opponent">Adversaire</th>
            <th colspan="2">Epreuve</th>
            <th rowspan="2"><span class='victory'>Gain</span><br/><span class="defeat">Perte</span></th>
            <?php if($sf_user->isAuthenticated() && $sf_user->getGuardUser()->getId() == $member_info[ 'sfGuardUserProfile' ][ 'User' ][ 'id' ]){?>
                <td colspan="2" rowspan="2" class="actions"></td>
            <?php }?>
        </tr>
        <tr>
            <th class="opponent">Nom Prénom</th>
            <th class="opponent">Points mensuels</th>
            <th>Code</th>
            <th>Coefficient</th>
        </tr>
    </thead>
    <tbody>

        <?php
        $i= ($pager->getPage()-1) * sfConfig::get('app_matchList_max_matchs') + 1;
        $totalResultGain = 0;
        $resultGain = 0;
        if($nbMatchs > 0){

            // find Mensual Point
            foreach($licensee_match_list as $match){

                $lic_match_date = new DateTime();
                $month = date('m', strtotime($match['datetime_match']));
                $year = date('Y', strtotime($match['datetime_match']));
                $lic_match_date->setDate($year, $month, '01');
                $lic_match_date->setTime(0, 0, 0);

                $mensualPoints = 0;
                foreach($licenseePointByMonthArray as $datetimeByMonth => $licenseePointsByMonth){
                    if($datetimeByMonth < $lic_match_date->getTimestamp()){
                        $mensualPoints = $licenseePointsByMonth;
                    }
                }

                ?>
                <tr class="l_<?php echo $i%2 ?>">
                    <td><?php echo $i; ?></td>
                    <td <?php echo ($match[ 'status' ] == 'V' ? "class='victoryBg'" : "class='defeatBg'") ?>><?php echo $match[ 'status' ] ?></td>
                    <td><?php echo date('d/m/Y', strtotime($match[ 'datetime_match' ])) ?></td>
                    <td><?php echo $match[ 'opponent_lastname' ] . " " . $match[ 'opponent_firstname' ] ?></td>
                    <td><?php echo $match[ 'opponent_point' ] ?></td>
                    <td><?php echo $match[ 'MatchType' ][ 'id' ] ?></td>
                    <td><?php echo $match[ 'MatchType' ][ 'coefficient' ] ?></td>
                    <td>
                        <?php
                            $gain = LicenseePoint::getGain($mensualPoints, $match['opponent_point'], $scoring_table, $match['status']);

                            $resultGain = $gain * $match[ 'MatchType' ][ 'coefficient' ];
                            $totalResultGain += $resultGain;
                            if($resultGain > 0){ ?>
                                <span class='victory'>+<?php echo number_format($resultGain, 2) ?></span>
                            <?php
                            }else if($resultGain < 0){
                            ?>
                                <span class='defeat'><?php echo number_format($resultGain, 2) ?></span>
                            <?php
                            }else{
                            ?>
                                <span>+<?php echo number_format($resultGain, 2); ?></span>
                            <?php }?>
                    </td>
                    <?php if($sf_user->isAuthenticated() && $sf_user->getGuardUser()->getId() == $member_info[ 'sfGuardUserProfile' ][ 'User' ][ 'id' ]){?>
                        <td class="modification">
                            <a href="<?php echo url_for('@member_myMatchList_edit?id=' . $match['id']) ?>">
                                <img src="/sfDoctrinePlugin/images/edit.png" alt="Modifier" title="Modifier"/>
                            </a>
                        </td>
                        <td class="delete">
                            <a href="<?php echo url_for('@member_myMatchList_delete?id=' . $match['id']) ?>">
                                <img src="/sfDoctrinePlugin/images/delete.png" alt="Supprimer" title="Supprimer"/>
                            </a>
                        </td>
                    <?php }?>
                </tr>
                <?php
                $i++;
                }
            }else{?>
                <tr>
                    <td colspan="8" class="noMatch">Aucun match enregistré pour cette saison.</td>
                </tr>
        <?php }?>

    </tbody>

</table>
<div class="navPages"><?php echo pager_navigation($licensee_match_list, url_for('@member_matchList?name_slug=' . $member_info[ 'slug' ])); ?></div>


<div class="push_float"></div>